package PowerWord.Core.Plan;

import java.util.Random;

import PowerWord.Core.Data.FullInfo;

public class MultiExercise extends Exercise {
	
	class ExerciseData{
		FullInfo[] items = new FullInfo[GroupFullSize];
		int answers[] = new int[GroupFullSize];
	}
	
	public MultiExercise(int groupSize) {
		super(groupSize);
		data = new ExerciseData();
	}
	
	ExerciseData data = null;
	
	public String GetWord(int index){
		return data.items[index].word;
	}
	
	public String GetTranslation(int index){
		return data.items[index].data.GetSimpContent();
	}
	
	public int[] GetAnswers(){
		return data.answers;
	}
	
	public void SaveScore(int[] usrAnswers){
		for(int i=0; i<GroupFullSize; ++i){
			if(usrAnswers[i] == data.answers[i]){
				++score;
			}
			else{
				AddWrongWord(data.answers[i]);
			}
		}
	}
	
	private void PrepareAnswers(){
		int groupSize = GetCurGroupSize();
		for(int i=0; i<groupSize; ++i){
			data.answers[i] = i;
		}
		Random seed = new Random();
		for(int i=0; i<groupSize; ++i){
			int pos = seed.nextInt(GroupFullSize - i);
			int value = data.answers[pos];
			data.answers[pos] = data.answers[i];
			data.answers[i] = value;
		}
	}
	
	public void Prepare(){
		PrepareAnswers();
		int basePos = curGroup * GroupFullSize;
		int endPos = GetCurGroupSize();
		for(int i=0; i<endPos; ++i){
			data.items[i] = new FullInfo();
			data.items[i].word = Controller.Instance().GetItem(basePos+i);
			data.items[i].data = Controller.Instance().GetData(basePos+data.answers[i]);
		}
	}
}
